﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：ChildUserInfo.cs
    // 文件功能描述：子用户，子用户信息与权限设置
    // 创建标识：吕海乐2009-03-12
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：页面作废 2009-6-4 李刚
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;

using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using System.Collections.Generic;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin
{
	public partial class ChildUserInfo : BasePage
	{
        public ChildUserInfo()
        {
            this.ModuleNo = Module.系统管理.ToString();
            this.PageOperateType = OperateType.子用户管理.ToString();
        }
        /// <summary>
        /// 页面加载时绑定子用户数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		protected void Page_Load(object sender, EventArgs e)
		{
            if (!this.IsPostBack)
            {
                string userId =Request["id"];
                IBLL_User iu = Factory.BLL_User();
                try
                {
                    //获取子用户基本信息和子用户权限
                    User user = iu.GetUserByUserID(userId);
                    lblChildUserId.Text = user.UserID;
                    txtUserName.Text = user.UserName;
                    IList<OperatingRule> operatingList = iu.GetUserOperatingRule(userId);
                    gvwChildUserRule.DataSource = operatingList;
                    gvwChildUserRule.DataBind();
                }
                catch(Exception ex)
                {
                    PageError(ex.Message,"ChildUserList.aspx");
                }

            }
		}


        /// <summary>
        /// 设置子用户信息及权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string userId = Request["id"];
            User user = new User();
            user.UserID = lblChildUserId.Text;
            user.UserName = txtUserName.Text;

            //获取用户权限
            List<OperatingRule> operatingRuleList = new List<OperatingRule>();
            string ids = Request["checkbox1"];
            if (!string.IsNullOrEmpty(ids))
            {
                string[] arraryIds = ids.Split(',');
                foreach (string Item in arraryIds)
                {
                    OperatingRule otr = new OperatingRule();
                    otr.OperatingRuleID = Item;
                    operatingRuleList.Add(otr);
                }
            }

            //update by [庄金冬] 20090314am  因UpdateUser方法有更新，故做修改【庄金冬】
            //if (iu.UpdateUser(user) && iu.SetUserOperatingRule(userId, operatingRuleList))
            //{
            //    HZ.Web.MessageBox.Show("保存成功！");
            //}
            //else
            //{
            //    HZ.Web.MessageBox.Show("保存失败！");
            //}
            
            //判断操作情况
            IBLL_User iu = Factory.BLL_User();
            try
            {
                int flag = iu.UpdateUser(user);
                if (flag == 0)
                {
                    HZ.Web.MessageBox.Show("用户信息更新失败！");
                    return;
                }
                if (flag == 1)
                {
                    try
                    {
                        bool isOk = iu.SetUserOperatingRule(userId, operatingRuleList);
                        if (isOk)
                        {
                            HZ.Web.MessageBox.AlertAndRedirect("用户信息更新成功！", "ChildUserList.aspx");
                        }
                        else
                        {
                            HZ.Web.MessageBox.Show("用户权限更新失败！");
                        }
                    }
                    catch(Exception ex)
                    {
                        PageError(ex.Message,"ChildUserList.aspx");
                    }
                }
                if (flag == 2)
                {
                    HZ.Web.MessageBox.Show("用户编号不存在！");
                    return;
                }            
            }
            catch(Exception ex)
            {
                PageError(ex.Message,"ChildUserList.aspx");
            }

            // update by [庄金冬] 20090314am  因UpdateUser方法有更新，故做修改【庄金冬】          
        }
	}
}
